//
//  YZNetworkManager+Quote.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/8.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZClient+Quote.h"

@implementation YZNetworkManager (Quote)

- (void) dealError:(NSError*) err
{
    if ([err code] == -1202) {
        self.quoteNetork.securityPolicy.allowInvalidCertificates = YES;
        self.quoteNetork.securityPolicy.validatesDomainName = NO;
        self.quoteNetork.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    }
}

- (void)qt_getAllStocksWithVersion: (NSString *)version success:(void (^)(NSURLResponse *response,id responseObject))success failure: (void (^)(NSError *error))failure
{
    NSMutableDictionary *param = [@{@"type":@"appAll"} mutableCopy];
    if (version.length > 0) {
        [param setObject:version forKey:@"version"];
    }
    
    [self.quoteNetork GET:URL_FOR_FETCH_ALL_STOCKS parameters:param progress:nil success:^(NSURLSessionDataTask * task, id responseObject) {
        
        if (success) {
            success(task.response, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        
        if (failure) {
            [self dealError:error];
            failure(error);
        }
    }];
}

- (void)qt_getRealtimeInfoWithStockCodes: (NSArray *)stockCodes success:(void (^)(NSURLResponse *response,id responseObject))success
                                 failure: (void (^)(NSError *error))failure
{
    NSString *codesStr = [stockCodes componentsJoinedByString: @","];
    NSDictionary *params = @{@"stockCode": codesStr, @"type":@"info"};
    
    [self.quoteNetork GET:URL_FOR_REAL_QUOTE parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(task.response, responseObject);
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        if (failure) {
            [self dealError:error];
            failure(error);
        }
    }];
}

- (void)qt_getTrendInfoWithStockCode:(NSString *)stockCode andBegin:(NSString *)begin success:(void (^)(NSURLResponse *response,id responseObject))success failure: (void (^)(NSError *error))failure
{
    NSDictionary *params = nil;
    if (begin == nil) {
        params = @{@"stockCode": stockCode, @"type":@"sline"};
    }
    else
    {
        params = @{@"stockCode": stockCode, @"timeLe": begin, @"type":@"sline"};
    }
    
    [self.quoteNetork GET:URL_FOR_TREND parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(task.response, responseObject);
        }
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            [self dealError:error];
            failure(error);
        }
    }];
}

- (void)qt_getKlineInfoWithStockCode: (NSString *)stockCode andBegin: (NSString *)begin andEnd: (NSString *)end success:(void (^)(NSURLResponse *response,id responseObject))success failure: (void (^)(NSError *error))failure
{
    NSDictionary *params = @{@"stockCode": stockCode, @"timeLe": begin, @"timeGe":end, @"type":@"kline"};
    
    [self.quoteNetork GET:URL_FOR_KLINE parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(task.response, responseObject);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            [self dealError:error];
            failure(error);
        }
    }];
}

-(void)qt_getKlineInfoWithStockCode:(NSString *)stockCode andType:(NSString *)type success:(void (^)(NSURLResponse *, id))success failure:(void (^)(NSError *))failure{

    NSDictionary *params = @{@"stockCode": stockCode, @"type":type};

    [self.quoteNetork GET:URL_FOR_KLINE parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(task.response, responseObject);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            [self dealError:error];
            failure(error);
        }
    }];
}

@end
